package paint;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

/**
 * Classe ControlerDessin<br>
 * @author Romain Bustos
 * @author Matthieu Perrin
 * @version 1.0
 */
public class ControlerDessin implements MouseListener, MouseMotionListener
{
    Dessin dessin;

    int pressX = 0;
    int pressY = 0;
    int id_forme = 0;

        int x2 =0;
        int y2 =0;


    public ControlerDessin(Dessin w)
    {
        this.dessin = w;
    }
    @Override
    public void mouseClicked(MouseEvent me) { }
    @Override
    public void mouseDragged(MouseEvent me) {  }
    @Override
    public void mouseExited(MouseEvent me) {  }
    @Override
    public void mouseEntered(MouseEvent me){  }
    @Override
    public void mouseMoved(MouseEvent me) {  }

    @Override
   public void mousePressed(MouseEvent me)
    {
    if (dessin.mode_selection)
        {
        int x = me.getX();
        int y = me.getY();
        pressX = x;
        pressY = y;

        for ( int i=0; i<dessin.formes.size(); i++ )
        {
             if ( dessin.formes.get(i) instanceof Rectangle)
            {
                int Xmin = ((Rectangle)dessin.formes.get(i)).getOrigine().getX();
                int Xmax = Xmin + ((Rectangle)dessin.formes.get(i)).getLarg();
                int Ymin = ((Rectangle)dessin.formes.get(i)).getOrigine().getY();
                int Ymax = Ymin + ((Rectangle)dessin.formes.get(i)).getLong();

                if ( x > Xmin && x < Xmax && y > Ymin && y < Ymax)
                {
                    for(int j = 0; j < dessin.formes.size(); j++)
                        dessin.formes.get(j).setSelect(false);
                    
                    dessin.formes.get(i).setSelect(true);

                dessin.repaint();
                id_forme = i;
                }
                else{
                    dessin.formes.get(i).setSelect(false);
                    dessin.repaint();
                }
            }
        if ( dessin.formes.get(i) instanceof Ovale)
        {
            int Xmin = ((Ovale)dessin.formes.get(i)).getOrigine().getX();
            int Xmax = Xmin + ((Ovale)dessin.formes.get(i)).getLarg();
            int Ymin = ((Ovale)dessin.formes.get(i)).getOrigine().getY();
            int Ymax = Ymin + ((Ovale)dessin.formes.get(i)).getLong();

            if ( x > Xmin && x < Xmax && y > Ymin && y < Ymax)
            {
                for(int j = 0; j < dessin.formes.size(); j++)
                    dessin.formes.get(j).setSelect(false);
                    
                    dessin.formes.get(i).setSelect(true);

                    dessin.repaint();
                    id_forme = i;
            }
        }
        }
        if ( me.getButton() == MouseEvent.BUTTON3)
        {
             JPopupMenu popup = new JPopupMenu();

             JMenuItem supp = new JMenuItem("Supprimer");
             JMenuItem dup = new JMenuItem("Dupliquer");
             JMenuItem arr = new JMenuItem("Arriere plan");
             popup.add(supp);
             popup.add(dup);
             popup.add(arr);

             popup.show(me.getComponent(), me.getX(), me.getY());

             supp.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        dessin.formes.remove(id_forme);
                        dessin.repaint();
                    }
                });

             dup.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                      
                       Forme f = dessin.formes.get(id_forme).createClone();
                       f.setCouleur(dessin.formes.get(id_forme).getCouleur());
                       dessin.formes.add(f);
                       dessin.repaint();
                      
                    }
                });

             arr.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        dessin.formes.add(0, dessin.formes.remove(id_forme));
                        dessin.repaint();
                    }
                });
        }
 }
 else
 {
    Click(me);
 }
 }
    public void mouseReleased(MouseEvent me)
    {
        if ( dessin.mode_selection)
        {
            int x = me.getX();
            int y = me.getY();
            int diferenceX = pressX -x;
            int diferenceY = pressY -y;

            //System.out.println(diferenceX + "\n" + diferenceY);
            if (dessin.formes.get(id_forme) instanceof Rectangle){
                    ((Rectangle)dessin.formes.get(id_forme)).P1.setX(((Rectangle)dessin.formes.get(id_forme)).P1.getX()-diferenceX);
                    ((Rectangle)dessin.formes.get(id_forme)).P2.setX(((Rectangle)dessin.formes.get(id_forme)).P2.getX()-diferenceX);
                    ((Rectangle)dessin.formes.get(id_forme)).P1.setY(((Rectangle)dessin.formes.get(id_forme)).P1.getY()-diferenceY);
                    ((Rectangle)dessin.formes.get(id_forme)).P2.setY(((Rectangle)dessin.formes.get(id_forme)).P2.getY()-diferenceY);

            } 
            if (dessin.formes.get(id_forme) instanceof Ovale){
                    ((Ovale)dessin.formes.get(id_forme)).P1.setX(((Ovale)dessin.formes.get(id_forme)).P1.getX()-diferenceX);
                    ((Ovale)dessin.formes.get(id_forme)).P2.setX(((Ovale)dessin.formes.get(id_forme)).P2.getX()-diferenceX);
                    ((Ovale)dessin.formes.get(id_forme)).P1.setY(((Ovale)dessin.formes.get(id_forme)).P1.getY()-diferenceY);
                    ((Ovale)dessin.formes.get(id_forme)).P2.setY(((Ovale)dessin.formes.get(id_forme)).P2.getY()-diferenceY);

            }       
            dessin.repaint();
        }

    }

    //public void mouseClicked(MouseEvent me)
    public void Click(MouseEvent me)
    {
        int x = me.getX();
        int y = me.getY();

        // Creation d'un segment
        if ( dessin.selection.type == 7)
        {
            if (dessin.attente == false)
            {
                x2=x;
                y2=y;
                dessin.attente = true;
            }
            else
            {
                Segment segment = new Segment(new Point(x, y), new Point(x2, y2));
                segment.couleur = dessin.selection.couleur;
                dessin.formes.add(segment);
                dessin.attente = false;
            }
        }

        // Creation d'un rectangle
        if ( dessin.selection.type == 1)
        {
            if (dessin.attente == false)
            {
                x2=x;
                y2=y;
                dessin.attente = true;
            }
            else
            {
                RectanglePlein rectangle = new RectanglePlein(new Point(x, y), new Point(x2, y2));
                rectangle.couleur = dessin.selection.couleur;
                dessin.formes.add(rectangle);
                dessin.attente = false;
            }
        }

        // Creation d'un rectangle vide
        if ( dessin.selection.type == 2)
        {
            if (dessin.attente == false)
            {
                x2=x;
                y2=y;
                dessin.attente = true;
            }
            else
            {
                RectangleVide rectangle = new RectangleVide(new Point(x, y), new Point(x2, y2));
                rectangle.couleur = dessin.selection.couleur;
                dessin.formes.add(rectangle);
                dessin.attente = false;
            }
        }

        // Creation d'un rectangle vide
        if ( dessin.selection.type == 5)
        {
            if (dessin.attente == false)
            {
                x2=x;
                y2=y;
                dessin.attente = true;
            }
            else
            {
                //Rectangle rectangle = new Rectangle(new Point(x, y), new Point(x2, y2), (double)(x2-x), (double)(y2-y));
                RoundRectangleVide rectangle = new RoundRectangleVide(new Point(x, y), new Point(x2, y2), 0, 0);
                rectangle.couleur = dessin.selection.couleur;
                dessin.formes.add(rectangle);
                dessin.attente = false;
            }
        }

        // Creation d'un rectangle vide
        if ( dessin.selection.type == 6)
        {
            if (dessin.attente == false)
            {
                x2=x;
                y2=y;
                dessin.attente = true;
            }
            else
            {
                //Rectangle rectangle = new Rectangle(new Point(x, y), new Point(x2, y2), (double)(x2-x), (double)(y2-y));
                RoundRectanglePlein rectangle = new RoundRectanglePlein(new Point(x, y), new Point(x2, y2), 0, 0);
                rectangle.couleur = dessin.selection.couleur;
                dessin.formes.add(rectangle);
                dessin.attente = false;
            }
        }

        // Creation d'un rond
        if ( dessin.selection.type == 3)
        {
            if (dessin.attente == false)
            {
                x2=x;
                y2=y;
                dessin.attente = true;
            }
            else
            {
                //OvalePlein ovale = new OvalePlein(new Point(x, y), new Point(x2, y2));
                OvalePlein ovale = new OvalePlein(new Point(x, y), new Point(x2, y2));
                ovale.couleur = dessin.selection.couleur;
                dessin.formes.add(ovale);
                dessin.attente = false;
            }
        }

        // Creation d'un rond vide
        if ( dessin.selection.type == 4)
        {
            if (dessin.attente == false)
            {
                x2=x;
                y2=y;
                dessin.attente = true;
            }
            else
            {
                OvaleVide ovale = new OvaleVide(new Point(x, y), new Point(x2, y2));
                ovale.couleur = dessin.selection.couleur;
                dessin.formes.add(ovale);
                dessin.attente = false;
            }
        }

        // Creation d'un point
        if ( dessin.selection.type == 8 )
        {
            Point point = new Point(x, y);
            point.couleur = dessin.selection.couleur;
            dessin.formes.add(point);
        }

        dessin.repaint();

    }

}
